//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    id1: "1",
    id2: "2",
    id3: "3",
    id4: "4",
    id5: "5",
    id6: "6",
    id7: "7",
    id8: "8",
    id9: "9",
    id10: "0",
    id11: "+",
    id12: "-",
    id13: "÷",
    id14: "×",
    id15: "=",
    id16: ".",
    id17: "negativo",
    id18: "back",
    id19: "clear",
    id20: "history",
    screenDate:"0",
    lsatIsOperator:false,
    arr:[],
    logss:[],
    userInfo: {}
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    })
  },
  clickButton:function(event){
    var data,id=event.target.id
    if(id==this.data.id15){//等于
      var data=this.data.screenDate;
      if(data=="0"){//判断是否为零
          return
      }
      var lastNumber=data.substring(data.length-1,data.length);
      if(isNaN(lastNumber)){
          return
      }
      var num="";
      var lsatOperator;
      var arr=this.data.arr;
      var option=[];
      for(var i in arr){
        if(isNaN(arr[i])==false||arr[i]==this.data.id16||arr[i]==this.data.id17){
          num+=arr[i]
        }else{
          lsatOperator=arr[i];
          option.push(num);
          option.push(arr[i]);
          num="";
        }
      }
      option.push(Number(num));
      var result=Number(option[0])*1.0;
      for(var i=1;i<option.length;i++){
          if(isNaN(option[i])){
              if(option[i]==this.data.id11){
                result+=Number(option[i+1])
              }else if(option[1]==this.data.id12){
                result-=Number(option[i+1])
              }else if(option[1]==this.data.id13){
                result/=Number(option[i+1])
              }else if(option[1]==this.data.id14){
                result*=Number(option[i+1])
              }
          }
      }
      this.data.logss.unshift(data+"="+result);
      wx.setStorageSync('calls',this.data.logss);
      this.data.arr.length=0;
      this.data.arr.push(result);
      this.setData({screenDate:result+""});
    }else if(id==this.data.id17){//正负号
      var data=this.data.screenDate;
      if(data=="0"){//判断是否为零
          return
      }
      var first=data.substring(0,1);
      if(first=="-"){//判断是否已是负数
          data=data.substring(1,data.length);
          this.data.arr.shift();
      }else{
          data="-"+data;
          this.data.arr.unshift("-");
      }
      this.setData({screenDate:data});
    }else if(id==this.data.id18){//退格
      var data=this.data.screenDate;
      if(data=="0"){
          return
      }
      data=data.substring(0,data.length-1);
      if(data==""||data=="-"){
          data=0
      }
      this.setData({screenDate:data});
      this.data.arr.pop();
    }else if(id==this.data.id19){//清屏
      this.setData({screenDate:"0"});
      this.data.arr.length=0;
    }else if(id==this.data.id20){//历史
      wx.navigateTo({
        url: '../history/history'
      })
    }else{
        if(id==this.data.id11||id==this.data.id12||id==this.data.id13||id==this.data.id14){
              if(this.data.lsatIsOperator==true||this.data.screenDate==0){
                  return
              }
        }
        var sd=this.data.screenDate;
        if(sd=="0"){
              data=id
        }else{
              data=sd+id
        }
        this.setData({screenDate:data});
        this.data.arr.push(id);
        if(id==this.data.id11||id==this.data.id12||id==this.data.id13||id==this.data.id14){
            this.setData({lsatIsOperator:true});
        }else{
            this.setData({lsatIsOperator:false});
        }
    }
    // 日志跳转
  }
})